/*
 * Copyright 2009 Colin Prinn
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package net.recursv.motific.at.runner;

import net.recursv.motific.at.command.AtCommand;
import net.recursv.motific.at.impl.__MIDlet;

import java.util.Vector;


/**
 * Created by: recursv
 * Date: 9/08/2009
 * Time: 12:09:02 AM
 */
public class AtRunner implements AtScriptObserver {
    private boolean _alive = true;
    private ScriptIterator _iterator;
    private Vector _observers = new Vector();
    private AtScript _script;
    private Class _targetMidlet;

    public AtRunner(ScriptIterator iterator, Class targetMidlet) {
        _iterator = iterator;
        _targetMidlet = targetMidlet;
    }

    public void execute() {
        notifyStartOfTesting();
        myExecute();
    }

    private void myExecute() {
        try {
            _script = _iterator.next();
            if (!_script.isNull()) {
                _script.setup(this, (__MIDlet) _targetMidlet.newInstance());
                notifyStarting(_script);
                _script.execute();
            } else {
                notifyEndOfTesting();
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public void cease() {
        _alive = false;
        _observers = null;
        _iterator = null;
        _script = null;
    }

    public void notifyStartOfTesting() {
        for (int i = 0; i < _observers.size(); i++)
            if (_observers.elementAt(i) instanceof AtRunnerObserver)
                ((AtRunnerObserver) _observers.elementAt(i)).notifyStartOfTesting();
    }

    public void notifyEndOfTesting() {
        for (int i = 0; i < _observers.size(); i++)
            if (_observers.elementAt(i) instanceof AtRunnerObserver)
                ((AtRunnerObserver) _observers.elementAt(i)).notifyEndOfTesting();
    }

    public void notifyStarting(AtScript script) {
        for (int i = 0; i < _observers.size(); i++)
            if (_observers.elementAt(i) instanceof AtScriptObserver)
                ((AtScriptObserver) _observers.elementAt(i)).notifyStarting(script);
    }

    public void notifySuccess(AtCommand command) {
        for (int i = 0; i < _observers.size(); i++)
            if (_observers.elementAt(i) instanceof AtScriptObserver)
                ((AtScriptObserver) _observers.elementAt(i)).notifySuccess(command);
    }

    public void notifyFailed(AtCommand command, Throwable error) {
        for (int i = 0; i < _observers.size(); i++)
            if (_observers.elementAt(i) instanceof AtScriptObserver)
                ((AtScriptObserver) _observers.elementAt(i)).notifyFailed(command, error);
        notifyEndOfTesting();
    }

    public void notifyComplete(AtScript script) {
        for (int i = 0; i < _observers.size(); i++)
            if (_observers.elementAt(i) instanceof AtScriptObserver)
                ((AtScriptObserver) _observers.elementAt(i)).notifyComplete(script);

        script.tearDown();
        if (_alive)
            myExecute();
    }

    public void addListener(AtScriptObserver observer) {
        _observers.addElement(observer);
    }
}
